package cn.zhaozhiqi.user.controller;

import cn.zhaozhiqi.exception.BussinessException;
import cn.zhaozhiqi.user.domain.User;
import cn.zhaozhiqi.user.query.UserQuery;
import cn.zhaozhiqi.user.service.IUserService;
import cn.zhaozhiqi.util.PageList;
import cn.zhaozhiqi.util.R;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;
    //1.查询一条记录
    @GetMapping("/{id}")
    public R<User> getByID(@PathVariable("id")Long id){
        try {
            User user = userService.getByID(id);
            return R.ok(user);
        } catch (BussinessException e) {
            log.info("查询用户操作异常：{}",e.getMessage());
            return R.fail("查询失败");
        }catch (Exception e) {
            log.info("查询用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，查询失败");
        }
    }
    //2.1查询所有记录
    @GetMapping()
    public R<Void> getAll(){
        try {
            userService.getAll();
            return R.ok();
        } catch (BussinessException e) {
            log.info("查询用户操作异常：{}",e.getMessage());
            return R.fail("查询失败");
        }catch (Exception e) {
            log.info("查询用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，查询失败");
        }
    }
    //2.新增一条记录
    @PostMapping("/add")
    public R<Void> addData(@RequestBody User user){
        try {
            userService.addData(user);
            return R.ok("新增成功");
        } catch (BussinessException e) {
            log.info("新增用户操作异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }catch (Exception e) {
            log.info("新增用户系统异常：{}",e.getMessage());
            return R.fail();
        }
    }
    //3.修改一条记录
    @PostMapping("/update")
    public R<Void> updateByID(@RequestBody User user){
        try {
            userService.updateByID(user);
            return R.ok("修改成功");
        } catch (BussinessException e) {
            log.info("修改用户操作异常：{}",e.getMessage());
            return R.fail("修改失败");
        }catch (Exception e) {
            log.info("修改用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，修改失败");
        }
    }
    //4.删除一条记录
    @DeleteMapping("/delete/{id}")
    public R<Void> deleteByID(@PathVariable("id")Long id){
        try {
            userService.deleteByID(id);
            return R.ok("删除成功");
        } catch (BussinessException e) {
            log.info("删除用户操作异常：{}",e.getMessage());
            return R.fail("删除失败");
        }catch (Exception e) {
            log.info("删除用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，删除失败");
        }
    }
    //5.查询分页
    @PostMapping("/page")
    public R<PageInfo<User>> userLimit(@RequestBody UserQuery query){
        try {
            PageInfo<User> pageInfo = userService.userLimit(query);
            return R.ok(pageInfo);
        } catch (BussinessException e) {

            log.info("分页操作异常：{}",e.getMessage());
            return R.fail("删除失败");
        }catch (Exception e) {
            e.printStackTrace();
            log.info("分页系统异常：{}",e.getMessage());
            return R.fail("系统异常，分页系统异常");
        }
    }

    //6.删除全部
    @PostMapping("/batchdelete")
    public R<Void> batchdelete(@RequestBody List<Long> ids){
        try {
            userService.batchdelete(ids);
            return R.ok("删除成功");
        } catch (BussinessException e) {
            log.info("删除用户操作异常：{}",e.getMessage());
            return R.fail("删除失败");
        }catch (Exception e) {
            log.info("删除用户系统异常：{}",e.getMessage());
            return R.fail("系统异常，删除失败");
        }
    }

}
